package com.client.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.client.bean.BirdList;
import com.client.bean.BirdMessage;
import com.client.common.SpinnerProgressDialog;
import com.client.db.DataService;
import com.client.task.BirdMessagesTask;
import com.client.task.UploadBirdClickNumMessagesTask;
import com.clinet.cache.AsyncImageLoader;
import com.clinet.cache.Log;

public class CollectionGridAct extends BaseAct {

	private Context mContext = CollectionGridAct.this;
	public static AsyncImageLoader mAsyncImageLoader = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.collectiongridact);
		initBottomBar();
		mAsyncImageLoader = new AsyncImageLoader();
		init();
		onclick();
		initNavigation();
//		getData();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData();
	}
	private void getData() {
		SpinnerProgressDialog.showDialog(this,"正在获取数据,请稍后...");
		BirdMessagesTask.Callback callback = new BirdMessagesTask.Callback() {
			@Override
			public void onComplete(final Integer result) {

				SpinnerProgressDialog.dismissDialog();
				switch (result) {
				case 1:
					initGridView();
					break;
				case 0:

					break;
				}
			}
		};
		mBirdList.key="";
		mBirdMessagesTask = new BirdMessagesTask(mContext, callback);
		mBirdMessagesTask.execute(mBirdList);

	}

	private BirdMessagesTask mBirdMessagesTask = null;

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mAsyncImageLoader.clearImageMap();
		mAsyncImageLoader.shutdownThreadPool();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = false;
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			messageUser();
			flag = true;
			break;
		default:
			flag = super.onKeyDown(keyCode, event);
			break;
		}
		return flag;
	}

	private String[] itmes = { "鸣禽", "鸣禽1", "鸣禽2", "鸣禽2", "鸣禽2", "鸣禽2", "鸣禽2" };

	private void initNavigation() {

		LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		mLayoutParams.rightMargin = 20;
		mLayoutParams.leftMargin = 20;
		mLayoutParams.gravity = Gravity.CENTER_VERTICAL;
		for (String temp : itmes) {
			TextView textview_item = (TextView) layoutFlater.inflate(
					R.layout.textview_item, null);
			textview_item.setText(temp);
			textview_item.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext, BirdSearchAct.class);
					mContext.startActivity(intent);
				}
			});
			textitems.addView(textview_item, mLayoutParams);
		}

	}

	private ImageView  add=null;
	private void onclick() {
		
		add=(ImageView)findViewById(R.id.add);
		add.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DetaiAct.MODE=DetaiAct.ADDMODE;
				Intent intent = new Intent(mContext, DetaiAct.class);
				mContext.startActivity(intent);
			}
		});
		birdtype.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, UnunloadBirdAct.class);
				mContext.startActivity(intent);
			}
		});
		animal.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, UnunloadAnimalAct.class);
				mContext.startActivity(intent);
			}
		});

		unupload.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, UnunloadAct.class);
				mContext.startActivity(intent);
			}
		});
		takepic.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
			}
		});
	}

	private TextView birdtype = null;
	private TextView animal = null;
	private TextView unupload = null;
	private LinearLayout textitems = null;

	private ImageView takepic = null;

	private void init() {
		layoutFlater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		birdtype = (TextView) findViewById(R.id.birdtype);
		animal = (TextView) findViewById(R.id.animal);
		unupload = (TextView) findViewById(R.id.unupload);
		textitems = (LinearLayout) findViewById(R.id.textitems);
		takepic = (ImageView) findViewById(R.id.takepic);
	}

	// -----------------------------网格----------------------S

	public void update() {
		if (gridView != null) {
			mgridViewAdapter.notifyDataSetChanged();
			return;
		}
	}

	public void initGridView() {
		if (mBirdList == null || mBirdList.getTotalNum() <= 0) {
			return;
		}
		gridView = (GridView) findViewById(R.id.time_grid);
		mgridViewAdapter = new TimeGridAdapter();
		gridView.setAdapter(mgridViewAdapter);
		gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> mAdapter, View view,
					int position, long id) {
				Log.i("onItemLongClick------"+mBirdList.getItems().get(position).birdId);
				initPopupMenu(mBirdList.getItems().get(position));
				if(mDataService==null)
				{
					mDataService=new DataService(mContext);
				}
				 mUploadBirdClickNumMessagesTask=new UploadBirdClickNumMessagesTask(mContext,mDataService);
				 mUploadBirdClickNumMessagesTask.execute(mBirdList.getItems().get(position));
				 mDataService.updateBirdMessageClick(mBirdList.getItems().get(position));
				 return false;
			}
		});

	}

	private DataService mDataService=null;
	private UploadBirdClickNumMessagesTask mUploadBirdClickNumMessagesTask=null;

	private BirdList mBirdList = new BirdList();
	private GridView gridView = null;
	private TimeGridAdapter mgridViewAdapter = null;
	private LayoutInflater layoutFlater;

	public class TimeGridAdapter extends BaseAdapter {

		public TimeGridAdapter() {
			super();

		}

		public int getCount() {
			return mBirdList.getTotalNum();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderGridViewItem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.collection_grid_item, null);
				holder = new ViewHolderGridViewItem();
				holder.birdName = (TextView) convertView
						.findViewById(R.id.birdName);
				holder.birdImage = (ImageView) convertView
						.findViewById(R.id.birdImage);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderGridViewItem) convertView.getTag();
			}

			BirdMessage bean = mBirdList.getItems().get(position);
			holder.birdName.setText(bean.birdName);
			String url = bean.birdImage;
			holder.birdImage.setTag(url);
			// 载入图片
			CollectionGridAct.mAsyncImageLoader.getGridViewLogoItem(mContext,
					bean.birdImage, holder.birdImage, gridView);
			return convertView;
		}
	}

	private static class ViewHolderGridViewItem {
		public TextView birdName = null;
		public ImageView birdImage = null;
	}

	// -----------------------------网格----------------------E
}